package com.lanou3g.ptp.api.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lanou3g.ptp.base.service.BaseService;
import com.lanou3g.ptp.entity.Teacher;
import com.lanou3g.ptp.organize.service.MajorService;
import com.lanou3g.ptp.sso.service.SsoService;
import com.lanou3g.ptp.student.service.StudentService;
import com.lanou3g.ptp.teacher.service.TeacherService;
import com.lanou3g.ptp.utils.PtpResult;
import com.lanou3g.ptp.utils.PtpResultUtils;
import com.lanou3g.ptp.vo.Option;
import com.lanou3g.ptp.vo.TreeNode;
import com.lanou3g.ptp.warning.service.WarningService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 创建人: 武奇
 * 创建事件: 2019/8/6
 */
@Api(description = "下拉框选项接口")
@RestController
@RequestMapping("/option")
@Slf4j
public class CheckListController {

    @Reference
    private BaseService baseService;

    @Reference
    private TeacherService teacherService;

    @Reference
    private StudentService studentService;

    @Reference
    private SsoService ssoService;

    @Reference
    private MajorService majorService;

    @Reference
    private WarningService warningService;

    @ApiOperation("获取所有年级")
    @GetMapping("/grade")
    public PtpResult grades() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        if (calendar.get(Calendar.MONTH) < Calendar.AUGUST) {
            year--;
        }
        List<Option> options = new ArrayList<>();
        for (int i = 3; i >= 0; i--) {
            int grade = year - i;
            options.add(new Option(Long.valueOf(grade), grade));
        }
        return PtpResultUtils.ok(options);
    }

    @ApiOperation("获取所有学期")
    @GetMapping("/semester")
    public PtpResult semesters() {
        String pattern = "%s-%s 第%s学期";
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        List<Option> options = new ArrayList<>();
        String str1, str2;
        if (calendar.get(Calendar.MONTH) < Calendar.AUGUST) {
            // 8月之前
            str1 = String.format(pattern, year - 1, year, "二");
            str2 = String.format(pattern, year, year + 1, "一");
        } else {
            // 8月之后
            str1 = String.format(pattern, year, year + 1, "一");
            str2 = String.format(pattern, year, year + 1, "二");
        }
        options.add(Option.builder().name(str1).build());
        options.add(Option.builder().name(str2).build());

        return PtpResultUtils.ok(options);
    }

    @ApiOperation("获取所有实习单位信息")
    @GetMapping("/base")
    public PtpResult bases() {
        List<Option.BaseOption> options = this.baseService.findOptions();
        log.info("查询所有基地信息: {}", options);
        return PtpResultUtils.ok(options);
    }

    @ApiOperation("根据基地id查询所属的所有指导教师")
    @GetMapping("/base/{baseId}/teachers")
    public PtpResult baseTeachers(@PathVariable Long baseId) {
        List<Option> options = this.teacherService.optionsByBaseId(baseId);
        return PtpResultUtils.ok(options);
    }

    @ApiOperation("根据教学环节id查询所有参与教师")
    @GetMapping("/teachlink/{teachlinkId}/teachers")
    public PtpResult teachlinkTeachers(@PathVariable Long teachlinkId) {
        List<Option> options = this.teacherService.optionsByTeachlinkId(teachlinkId);
        return PtpResultUtils.ok(options);
    }

    @ApiOperation("根据当前登录的教师所属专业查询所有学生信息, 树形结构")
    @GetMapping("/major/students")
    public PtpResult majorStudents(@RequestHeader String token) {
        Teacher teacher = (Teacher) this.ssoService.tokenUser(token);
        Long majorId = teacher.getMajorId();
        List<TreeNode> tree = this.studentService.treeByMajorId(majorId);
        return PtpResultUtils.ok(tree);
    }

    @ApiOperation("根据当前登录的教师所属专业查询相关的所有老师")
    @GetMapping("/major/teachers")
    public PtpResult majorTeachers(@RequestHeader String token) {
        Teacher teacher = (Teacher) this.ssoService.tokenUser(token);
        Long majorId = teacher.getMajorId();
        List<Option> teachers = this.teacherService.optionsByMajorId(majorId);
        return PtpResultUtils.ok(teachers);
    }

    @ApiOperation("查询所有专业信息")
    @GetMapping("/majors")
    public PtpResult majors() {
        List<Option> options = this.majorService.options();
        return PtpResultUtils.ok(options);
    }

    @ApiOperation("按类型查询预警方案")
    @GetMapping("/warning-plans")
    public PtpResult warningPlans(Integer type) {
        List<Option> options = this.warningService.options(type);
        return PtpResultUtils.ok(options);
    }

}
